package com.hnevc.xiaoming.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.google.gson.internal.$Gson$Preconditions;
import com.hnevc.xiaoming.R;
import com.hnevc.xiaoming.adapter.NewsAdapter;
import com.hnevc.xiaoming.httpactivity.MyNewsActivity;
import com.hnevc.xiaoming.httpactivity.MyNewsContentActivity;
import com.hnevc.xiaoming.httpbean.NewsBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class FragmentHomePage extends Fragment {

    private View inflate;
//    private Activity activity;
    private ListView listView;
    Handler handler = new Handler();
    private static final String TAG = "FragmentHomePage";
    private List<NewsBean.ResultBean.DataBean> list;
    private NewsBean newsBean;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_homepage_layout, container, false);
        initView();
        initData();

        return inflate;
    }

//    @Override
//    public void onAttach(@NonNull Context context) {
//        super.onAttach(context);
//        activity = (Activity) getContext();
//    }

    private void initData() {

        new Thread(new Runnable() {
            @Override
            public void run() {

                OkHttpClient client = new OkHttpClient();

                RequestBody formBoby = new FormBody.Builder()
                        .add("type","top")
                        .add("key","368cf096b8c3c4b53dbc664c686b3a6d")
                        .build();

                Request request = new Request.Builder()
                        .url("http://v.juhe.cn/toutiao/index")
                        .post(formBoby)
                        .build();

                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure: "+e.getMessage() );
                    }

                    @Override
                    public void onResponse(Call call, final Response response) throws IOException {

                                if (!response.isSuccessful()) {
                                    Toast.makeText(getContext(), "请求失败！", Toast.LENGTH_SHORT).show();
                                }
//                                Log.e(TAG, "run: "+response );
//                                Toast.makeText(getContext(), "aaaaaaaaaaaaaaaaa", Toast.LENGTH_SHORT).show();
                                Gson gson = new Gson();
                                newsBean = gson.fromJson(response.body().string(), NewsBean.class);
                         handler.post(new Runnable() {
                            @Override
                            public void run() {

                                list =newsBean.getResult().getData();
                                NewsAdapter newsAdapter = new NewsAdapter(list, getContext());
                                listView.setAdapter(newsAdapter);

                            }
                        });

                    }
                });
            }
        }).start();

    }

    private void initView() {

        listView = inflate.findViewById(R.id.news_okhttp);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                NewsBean.ResultBean.DataBean bean = list.get(position);
                Toast.makeText(getContext(),""+bean.getAuthor_name(),Toast.LENGTH_LONG).show();
                Intent intent = new Intent();
                intent.setClass(getContext(), MyNewsContentActivity.class);
                intent.putExtra("content",bean.getUrl()+"");
                startActivity(intent);
            }
        });


    }
}
